
-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "gpio2demo"
VERSION = "1.0.0"

log.info("main", PROJECT, VERSION)

-- sys库是标配
_G.sys = require("sys")

if wdt then
    --添加硬狗防止程序卡死，在支持的设备上启用这个功能
    wdt.init(9000)--初始化watchdog设置为9s
    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
end

local led1=pin.PB08
local led2=pin.PB09
local led3=pin.PB10

gpio.setup(led1,0)
gpio.setup(led2,0)
gpio.setup(led3,0)

function ledd(pp)
	gpio.toggle(pp)
end
function leda()
	gpio.toggle(led1)
	gpio.toggle(led2)
	gpio.toggle(led3)
end

function ss(xx,yy)
	for i=1,xx,1 do
	print('off')
	gpio.set(led1,0)
	gpio.set(led2,0)
	gpio.set(led3,0)
	print('on')
	gpio.set(led1,1)
	gpio.set(led2,1)
	gpio.set(led3,1)
	sys.wait(yy)
	print('off')
	gpio.set(led1,0)
	gpio.set(led2,0)
	gpio.set(led3,0)
	sys.wait(yy)
	end
end

local button_timer_outtime = 10 --按键定时器: 10ms
local button_shake_time = 1     --按键消抖时间: button_shake_time*button_timer_outtime
local button_long_time = 100    --按键长按时间: button_shake_time*button_timer_outtime

local button_detect = true      --按键检测开关--
local button_state = false		--状态
local button_cont = 0
--[[]]
local key1 = pin.PA01 -- 左
local key2 = pin.PA07 -- 上
local key3 = pin.PA04 -- 中
local key4 = pin.PA00 -- 右
local key5 = pin.PB11 -- 下

-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(key1, 20,1)
end
-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(key2, 20,1)
end
-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(key3, 20,1)
end
-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(key4, 20,1)
end
-- 若固件支持防抖, 启用防抖
if gpio.debounce then
    gpio.debounce(key5, 20,1)
end

gpio.setup(key1,function ()
	zuo=1
--	print('左')
end,gpio.PULLUP,gpio.FALLING)
gpio.setup(key2,function ()
	shang=1
--	print('上')
end,gpio.PULLUP,gpio.FALLING)
gpio.setup(key3,function ( ... )
	zhong=1
--	print('中')-- body
end,gpio.PULLUP,gpio.FALLING)
gpio.setup(key4,function ()
	you=1
--	print('右')-- body
end,gpio.PULLUP,gpio.FALLING)
gpio.setup(key5,function ()
	xia=1
--	print('下')
end,gpio.PULLUP,gpio.FALLING)
--[[

sys.taskInit(function()
		ss(3,100)
	
end)
]]        
gpio.setup(pin.PB00,0)
--[[
sys.taskInit(function()
   -- while 1 do
        -- 仿呼吸灯效果
     --   log.info("pwm", ">>>>>")
        for i = 100,1,-1 do
            pwm.open(0, 1000, 100-i) -- 频率1000hz, 占空比0-100
            sys.wait(20)
			print(i)
        end
      --  sys.wait(1000)
        for i = 100,1,-1 do
            pwm.open(0, 1000, i)
            sys.wait(20)
			print(i)
        end
      --  sys.wait(1000)
  --  end
end)
]]
abc=0
function aaa(bbb)
	abc=abc+bbb
	if abc > 100 then
		abc=0
	end
	return abc
end

function pp(xx)
	pwm.open(0,1000,xx)
	print(xx)
--	sys.wait(100)
	
end
sys.taskInit(function()
	
end)
sys.taskInit(function()
	while 1 do

		if shang==1 then
			shang =0;
			print('1')
			ledd(led1)
		--	break
		end
		if xia==1 then
			xia=0
			print('2')
			ledd(led2)
		--	break
		end
		if zuo==1 then
			zuo=0
			pp(aaa(10))
		--	ss(5,300)
		--	print('3')
		--	break
		end
		if you==1 then
			you=0
			pp(aaa(20))
		--	ss(5,100)
		--	print('4')
		--	break
		end
		if zhong==1 then
			zhong=0
			--ledd(led3)
			--print('5')
		--	break
		end

		sys.wait(1)
	end
end)

sys.run()
-- sys.run()之后后面不要加任何语句!!!!!
